#!/bin/sh
#
# Copyright (C) 2017 ~ 2018 Deepin Technology Co., Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# Start installer in auto-install mode

auto_install_mode=0
install_path=''
locale=''

parse_kernel_cmdline() {
  for item in $(cat /proc/cmdline); do
    case $item in
      auto-deepin-installer)
        auto_install_mode=1
        ;;
      install-path=*)
        install_path="${item##*=}"
        ;;
      deepin-installer/locale=*)
        locale="${item##*=}"
        ;;
    esac
  done
}

main() {
  parse_kernel_cmdline

  if [ $auto_install_mode ]; then
    #fix install_path
    #if use boot config, iso mount on /lib/live/mount/findiso
    local iso_root=/lib/live/mount/findiso
    install_path=$iso_root/$install_path

    local lang
    lang=$(echo $locale | awk -F '.' '{print $1}')
    # TODO(xushaohua): Append $lang to conf file.
    local installer_conf=$install_path/install/deepin-installer.conf
    local installer_log=$install_path/install/deepin-installer.log

    xinit /usr/bin/deepin-installer --conf ${installer_conf} \
      --log ${installer_log} --auto-install
  fi
}

main
